package controller;

import bean.Emp;
import com.alibaba.fastjson.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;


@Controller
@RequestMapping("/first") //网址，这是springmvc的注解
public class FirstController {

    /*springmvc方法的基础写法
    public String 方法名(参数列表){

    }
*/
    @RequestMapping("/a")
    public String a() {
        System.out.println("进入了FirstController类的a方法");
        // 跳转到那个页面
        return "index";
    }

    @GetMapping("/b")//只能接收get请求
    public String b(int a, String b, char c, double d) {
        System.out.println("进入了FirstController类的b方法");
        System.out.println("接收的参数:" + a + "," + b + "," + c + "," + d);
        return "index";
    }

    //接收封装好的参数
    @GetMapping("/c")
    public String c(Emp emp) {
        System.out.println("进入了FirstController类的c方法");
        System.out.println(emp);
        return "index";
    }

    //手动映射
    @GetMapping("/d")
    public String d(@RequestParam(name = "p", defaultValue = "1") Integer page) {
        System.out.println("进入了FirstController类的d方法");
        System.out.println("param:" + page);
        return "index";
    }

    //重定向
    @GetMapping("/e")
    public String e() {
        System.out.println("进入了FirstController类的e方法");
        return "redirect:/1.jsp";
    }

    //使用ServletAPI
    @GetMapping("/f")
    public String f(HttpServletRequest request, HttpSession session) {
        System.out.println("进入了FirstController类的f方法");
        request.setAttribute("A", "aaa");
        session.setAttribute("B", "bbb");
        session.getServletContext().setAttribute("C", "ccc");
        return "1";
    }

    //Ajax操作 方法1
    @GetMapping("/g1")
    public void g1(HttpServletResponse response) throws IOException {
        PrintWriter out = response.getWriter();
        Emp emp = new Emp();
        emp.setEname("ann");
        emp.setSal(7900.0);
        String json = JSONArray.toJSONString(emp);
        out.print(json);
    }

    //Ajax操作方法2 最常用※※※※※
    @GetMapping(value = "/g2", produces = {"application/json;charset=utf-8"})
    @ResponseBody //告诉它return的是数据而不是路径
    public String g2() {
        Emp emp = new Emp();
        emp.setEname("张三");
        emp.setSal(8888.0);
        emp.setJob("程序员");
        String json = JSONArray.toJSONString(emp);
        return json;
    }

    //了解的内容  只需要知道Model和View的概念
    @GetMapping("/h")
    public ModelAndView h(ModelAndView mv){
        //模型
        Map<String,Object> model=mv.getModel();//这个就是request容器
        model.put("A","模型存放的内容:aaa");
        //视图
        mv.setViewName("1");//根据图解析器合作产生了最终的页面
        return mv;
    }
}
